#include "BlockQueue.hpp"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "Task.hpp"

void *consumer(void *args)
{
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        sleep(1);
        Task t = bq->Pop();
        t.Execut();
        //std::cout << "消费了一个数据: " << data << std::endl;
        std::cout << "消费了一个任务: " << t.X() << "+" << t.Y() << "=" << t.Result() << std::endl;

    }
}

void *productor(void *args)
{
    //int data = 1;
    int x = 1;
    int y = 1;
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(args);
    while (true)
    {
        //sleep(1);
        //std::cout << "生产了一个数据: " << data << std::endl;
        std::cout << "生产了一个任务: " << x  << "+" << y << "=?" << std::endl;

        //int data = 10;
        Task t(x, y);
        bq->Equeue(t);

        //data++;
        x++, y++;
    }
}

int main()
{
    BlockQueue<Task> *bq = new BlockQueue<Task>();
    //BlockQueue<task_t> *bq = new BlockQueue<task_t>();

    pthread_t c, p;

    pthread_create(&c, nullptr, consumer, bq);
    pthread_create(&p, nullptr, productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}